package com.le.bbs.controller;

import com.le.api.BoardReplyApi;
import com.le.api.BoardTopicApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("count")
public class TopicCountController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate; // 需要配置 Redis数据库

    @Autowired
    private BoardTopicApi boardTopicApi;

    /**
     * 取消主题点赞收藏（目前只取消用户里的记录
     */
    @RequestMapping(value = "clearTopicZan", method = RequestMethod.GET)
    public Map<String, Object> clearTopicZan(@RequestParam String type,
                                             @RequestParam Integer uid,
                                             @RequestParam Integer topicid){
        Map<String, Object> result = new HashMap<>();
        // 检测键 在Redis数据库中是否存在
        if (this.redisTemplate.opsForHash().hasKey("bbs-count", uid + "_" + type)){
            // 获取("cart_" + bearerToken)键的 哈希表的键集合 // 取出此人（bearerToken）所有的购物项
            List topicList = (List) this.redisTemplate.opsForHash().get("bbs-count", uid + "_" + type);

            if (!topicList.contains(topicid)){
                result.put("code", 0);
                result.put("msg", "已经取消了！");
                result.put("data", topicList);
                return result;
            }

            topicList.removeAll(Collections.singleton(topicid));
            // 存入数据
            this.redisTemplate.opsForHash().put("bbs-count", uid+"_"+type, topicList);

            result.put("code", 1);
            result.put("msg", "取消成功！");
            result.put("data", topicList);
        } else {
            result.put("code", 0);
            result.put("msg", "取消操作异常，请联系管理员！");
        }
        return result;
    }


    /**
     * 获取主题点赞数浏览数收藏数
     */
    @RequestMapping(value = "getTopic", method = RequestMethod.GET)
    public Map<String, Object> getTopic(@RequestParam String type,
                                        @RequestParam Integer topicid){
        Map<String, Object> result = new HashMap<>();
        if (this.redisTemplate.opsForHash().hasKey("bbs-count", topicid+"_"+type)){
            // entries() 取键值对（全部取到
            List uidList = (List) this.redisTemplate.opsForHash().get("bbs-count", topicid+"_"+type);
//            log.info("token为：" + bearerToken + "，其购物车为：" + cart);
            result.put("code", 1);
            result.put("num", uidList.size());
        } else {
            result.put("code", 0);
            result.put("num", 0);
        }
        return result;
    }

    /**
     * 记录主题点赞浏览收藏
     */
    @RequestMapping(value = "zanTopic", method = RequestMethod.GET)
    public Map<String, Object> zanTopic(@RequestParam String type,
                                        @RequestParam Integer uid,
                                        @RequestParam Integer topicid){
        Map<String, Object> result = new HashMap<>();


        Map<String, Object> resultMap = this.boardTopicApi.findTopicById(topicid);
        if ("0".equalsIgnoreCase(resultMap.get("code").toString())){
            result.put("code", 0);
            result.put("msg", "查无此主题" + topicid);
            return result; // 业务问题，不是http的问题 返回200
        }

        // 添加点赞
        // 取出这个主题有哪些用户点赞
        List uidList = (List) this.redisTemplate.opsForHash().get("bbs-count", topicid + "_" + type);

        List topicList = (List) this.redisTemplate.opsForHash().get("bbs-count", uid + "_" + type);

        if (uidList == null){
            uidList = new ArrayList<Integer>();
        }
        uidList.add(uid);

        if (topicList == null){
            topicList = new ArrayList<Integer>();
        }
        topicList.add(topicid);

        // 存入数据
        this.redisTemplate.opsForHash().put("bbs-count", topicid+"_"+type, uidList);
        this.redisTemplate.opsForHash().put("bbs-count", uid+"_"+type, topicList);

        result.put("code", 1);
        result.put("num", uidList.size());

        return result;
    }



//    public static void main(String[] args) {
//        List<Integer> myList = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 2, 5));
//        int targetValue = 2;
//
//        myList.removeAll(Collections.singletonList(targetValue));
//
//        System.out.println("删除后的列表：" + myList);
//    }

}